package com.gitee.mrj.aiboot.core.sf.generators.ui.widget;

import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.gitee.mrj.aiboot.annotation.config.KV;
import com.gitee.mrj.aiboot.annotation.config.sf.ui.widget.SFTreeSelectWidget;
import com.gitee.mrj.aiboot.core.sf.generators.FormDefinitionGenerator;
import com.gitee.mrj.aiboot.core.sf.generators.UIForm;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;

@Component
public class SFTreeSelectWidgetGenerator implements FormDefinitionGenerator {
    @Override
    public void generate(UIForm root, Field field, UIForm node) {
        SFTreeSelectWidget widget = AnnotatedElementUtils.findMergedAnnotation(field, SFTreeSelectWidget.class);
        generate(node, widget);
    }

    private void generate(UIForm node, SFTreeSelectWidget widget) {
        ObjectNode ui = node.getUi();
        ui.put("widget", "tree-select");
        putString(ui, "placeholder", widget.placeholder());
        ui.put("allowClear", widget.allowClear());
        ui.put("dropdownMatchSelectWidth", widget.dropdownMatchSelectWidth());
        if (ArrayUtils.isNotEmpty(widget.dropdownStyle())) {
            ObjectNode obj = putObject(ui, "dropdownStyle");
            for (KV kv : widget.dropdownStyle()) {
                obj.put(kv.key(), kv.value());
            }
        }
        putString(ui, "dropdownClassName", widget.dropdownClassName());
        ui.put("multiple", widget.multiple());
        ui.put("hideUnMatched", widget.hideUnMatched());
        ui.put("checkable", widget.checkable());
        ui.put("checkStrictly", widget.checkStrictly());
        ui.put("showIcon", widget.showIcon());
        ui.put("showExpand", widget.showExpand());
        ui.put("showLine", widget.showLine());
        ui.put("defaultExpandAll", widget.defaultExpandAll());
        putString(ui, "notFoundContent", widget.notFoundContent());
        if (ArrayUtils.isNotEmpty(widget.expandedKeys())) {
            ArrayNode obj = putArray(ui, "expandedKeys");
            for (String str : widget.expandedKeys()) {
                obj.add(str);
            }
        }
        putInt(ui, "maxTagCount", widget.maxTagCount());
        putInt(ui, "virtualItemSize", widget.virtualItemSize());
        putInt(ui, "virtualMaxBufferPx", widget.virtualMaxBufferPx());
        putInt(ui, "virtualMinBufferPx", widget.virtualMinBufferPx());
        putString(ui, "virtualHeight", widget.virtualHeight());
    }


    @Override
    public Class[] supports() {
        return new Class[]{SFTreeSelectWidget.class};
    }
}
